* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	
	}
	
	use $interim_data, clear


* Ratio at unit-watch level ----------------------------------------------------
	
	* Endogeneous ratio at monthly level for each unit-watch
	use "${ANA_endo}", clear
	bys year_month mod_strata: keep if _n == 1
	keep year_month mod_strata ana_ratio_treatment_all ana_ratio_treatment_sample
	replace ana_ratio_treatment_all = 100 * ana_ratio_treatment_all

	* Exogenous ratio at randomization
	gen x = ana_ratio_treatment_all if (year_month==202002)	
	bys mod_strata: egen ana_ratio_treatment_all_exo = max(x)
	drop x
	
		/*Note:
		The data set "ANA endo" include CPD's ANA attendace information.
		This information is used to identify the unt-watch (strata) that the 
		officer spends the most time in  given month. We code this variable 
		into the field "mod_strata" */
		
* Prepare data for regression --------------------------------------------------

	* Attach the crime rate data
	merge 1:1 year_month mod_strata using "${Crime-UnitWatch}", nogen keep(2 3)
	
	* Calculate Rate per 1K
	foreach crime in all violent {
	gen `crime'per1K = 1000* `crime'_crimes / population
	}
	
	* Identify different period before and after the training
	gen pre_long 	= inrange(year_month, 201902, 202001) 
	gen pre_short 	= inrange(year_month, 201910, 202001)
	gen post_short 	= inrange(year_month, 202101, 202104) 
	gen post_long 	= inrange(year_month, 202101, 202112)

* Regression -------------------------------------------------------------------
	
	foreach m in ana_ratio_treatment_all_exo {
	foreach o in allper1K violentper1K {

	foreach c in pre_long pre_short post_long post_short {
		qui: sum `o' if chunk_`c' == 1
		local x = r(mean)
		qui: reg `o' `m' i.year_month if `c'==1, vce(cluster mod_strata)
		qui: test `m'
		mat add = ., _b[`m'], _se[`m'], r(p),., e(N)
		mat `o'_`c'_m1 = add
		mat li `o'_`c'_m1
	}
	}
	}
	

* Save results as table --------------------------------------------------------

	* Table B22
	gl month_segments  m1
	table_start_horizontal ${report_folder}/Admin_App_crime_rates.txt ///
	"Effects on Crime Outcomes" 220 "no_cm_with_n"
	midrule
	
	file write table "\\ \multicolumn{6}{c}{\textbf{Panel A: Crimes per 1,000 persons}}  \\" _n
	midrule
	file write table "\multicolumn{6}{l}{\textbf{Pre-Training Periods}}  \\" _n
		write_horizontal 	allper1K_pre_short	1	"October 2019 - January 2020" 	CoSePvN
		write_horizontal 	allper1K_pre_long	1	"February 2019 - January 2020" 	CoSePvN
	file write table "\\ \multicolumn{6}{l}{\textbf{Post-Training Periods}}  \\" _n
		write_horizontal 	allper1K_post_short	1	"January 2021 - April 2021" 	CoSePvN
		write_horizontal 	allper1K_post_long	1	"January 2021 - December 2021" 	CoSePvN
		
	file write table "\\ \multicolumn{6}{c}{\textbf{Panel B: Violent Crimes per 1,000 persons}}  \\" _n 
	midrule
	file write table "\multicolumn{6}{l}{\textbf{Pre-Training Periods}}  \\" _n
		write_horizontal 	violentper1K_pre_short	1	"October 2019 - January 2020" 	CoSePvN
		write_horizontal 	violentper1K_pre_long	1	"February 2019 - January 2020" 	CoSePvN
	file write table "\\ \multicolumn{6}{l}{\textbf{Post-Training Periods}}  \\" _n
		write_horizontal 	violentper1K_post_short	1	"January 2021 - April 2021" 	CoSePvN
		write_horizontal 	violentper1K_post_long	1	"January 2021 - December 2021" 	CoSePvN
	
	table_end "$end_crime_rate" Admin_Crime_Rate
	